package com.srwl.mytx.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;

import androidx.fragment.app.DialogFragment;

import com.srwl.mytx.domain.Contact;
import com.srwl.mytx.R;


public class AuthorityFragment extends DialogFragment {

    private OnFragmentInteractionListener mListener;
    private CheckBox checkBoxLocation;
    private CheckBox checkBoxTrace;
    private CheckBox checkBoxRelation;
    private CheckBox checkBoxCustomInfinite;
    private CheckBox checkBoxInfiniteChat;
    private CheckBox checkBoxlimteChat;
    private CheckBox checkBoxAdjustStructure;
    private View ll_Infinite;
    private Button btn_commit;
    private Contact contact;

    public AuthorityFragment() {
        // Required empty public constructor
    }


    public static AuthorityFragment newInstance(Contact contact) {
        AuthorityFragment fragment = new AuthorityFragment();
        Bundle args = new Bundle();
        args.putParcelable("contact", contact);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置dialog 样式,没有标题，宽度为 android alterDialog的默认显示宽度，
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_MinWidth);
        if (getArguments() != null) {
            contact = getArguments().getParcelable("contact");
        }

    }

//    @NonNull
//    @Override
//    public Dialog onCreateDialog(Bundle savedInstanceState) {
//        Dialog dialog = new Dialog(getActivity());
//        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // must be called before set content
//        dialog.setContentView(R.layout.fragment_authority);
//       // dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));
//        dialog.setCanceledOnTouchOutside(true);
//
//        // 设置宽度为屏宽、在屏幕中的位置
//        Window window = dialog.getWindow();
//        // window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
//        WindowManager.LayoutParams wlp = window.getAttributes();
//        wlp.gravity = Gravity.CENTER;
//        //设置全屏
//        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
//        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        float widthPixels= getContext().getResources().getDisplayMetrics().widthPixels;
//        //按屏幕宽度，设置固定的比例
//        wlp.width= (int) (widthPixels*0.8);
//        window.setAttributes(wlp);
//        return dialog;
//    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_authority, container, false);
        checkBoxLocation = view.findViewById(R.id.authority_location);
        checkBoxTrace = view.findViewById(R.id.authority_trace);
        checkBoxRelation = view.findViewById(R.id.relation_infinite);
        checkBoxCustomInfinite = view.findViewById(R.id.custom_infinite);
        checkBoxInfiniteChat = view.findViewById(R.id.infinite_chat);
        checkBoxlimteChat = view.findViewById(R.id.limit_chat);
        checkBoxAdjustStructure = view.findViewById(R.id.adjust_structure);
        ll_Infinite = view.findViewById(R.id.ll_custom_infinite);
        btn_commit = view.findViewById(R.id.btn_commit);
        initClickListener();
        return view;
    }

    private void initClickListener() {
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置我给对方的权限数据
                if (checkBoxLocation.isChecked()) {
                    contact.setTLocation("1");
                } else {
                    contact.setTLocation("0");
                }
                if (checkBoxTrace.isChecked()) {
                    contact.setTTrace("1");
                } else {
                    contact.setTTrace("0");
                }
                if (checkBoxRelation.isChecked()) {
                    contact.setTRelationInfinite("1");
                } else {
                    contact.setTRelationInfinite("0");
                }
                //通过接口 返回数据
                if (mListener != null) {
                    mListener.onSetAuthorityComplete(contact);
                }

            }
        });
    }

    public void setInteractionListener(OnFragmentInteractionListener listener) {
        mListener = listener;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onSetAuthorityComplete(Contact contact);
    }
}
